﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: ExpectationMaximizationBase.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2013-01-24 4:34 PM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
namespace BOL.Algorithms
{
    public abstract class ExpectationMaximizationBase : IVerbose, IIterative, IConvergeable
    {
        #region Private variables

        private double _criterion;
        private int _i;

        #endregion

        #region Public properties

        public bool IsVerbose { get; private set; }

        public int NumberOfIterations { get; private set; }

        public double Tolerance { get; private set; } 

        #endregion

        #region Constructor

        protected ExpectationMaximizationBase(int numberOfIterations, double tolerance, bool verbose = true)
        {
            NumberOfIterations = numberOfIterations;
            Tolerance = tolerance;
            IsVerbose = verbose;
        }

        #endregion

        #region Public methods

        public void Estimate()
        {
            _criterion = 0.0;
            _i = 0;

            while (_criterion > Tolerance && _i++ < NumberOfIterations)
            {
                Expect();

                Maximize();
            }
        }

        protected abstract void Expect();

        protected abstract void Maximize();

        #endregion
    }
}
